package com.xzy.spring.config;

import com.xzy.spring.business.repository.UserRepository;
import com.xzy.spring.demo.aware.MyAware;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

/**
 * @author xzy
 * @date 2021-02-13 20:39
 * 说明：自动装配
 */
@Configuration
@ComponentScan(value = {"com.xzy.spring.*"})
public class AutowiredConfig {

    @Bean
    public UserRepository userRepository1() {
        return new UserRepository("userRepository1");
    }

    @Bean
    public UserRepository userRepository2() {
        return new UserRepository("userRepository2");
    }

    @Bean
    public UserRepository userRepository3() {
        return new UserRepository("userRepository3");
    }

    /**
     * 首选组件：自动注入时，如果存在多个同类型的组件，优先注入该组件
     */
    @Primary
    @Bean
    public UserRepository userRepositoryPrimary() {
        return new UserRepository("userRepositoryPrimary");
    }

    /**
     * 获取Spring框架底层组件
     */
    @Bean
    public MyAware frameworkComponents() {
        return new MyAware();
    }
}
